package com.aysafety.iotdataplatformclient.sonicPulse.services.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.aysafety.core.common.DateUtils;
import com.aysafety.core.model.SonicPulseModel;
import com.aysafety.iotdataplatformclient.modelConfigurer.SonicPulseModelConfiger;
import com.aysafety.iotdataplatformclient.sonicPulse.dao.SonicPulseDao;
import com.aysafety.iotdataplatformclient.sonicPulse.services.SonicPulseService;

@Service
public class SonicPulseServiceImpl implements SonicPulseService {

    @Autowired
    SonicPulseModelConfiger sonicPulseModelConfiger;

    @Autowired
    SonicPulseDao sonicPulseDao;

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    private SonicPulseModel transformData(Map<String, Object> temp, Map<String, String> structure) {
        SonicPulseModel sonicPulseModel = new SonicPulseModel();
        for (String key : structure.keySet()) {
            String columnKey = structure.get(key);
            if ("EntId".equals(key)) {
                sonicPulseModel.setEntId(sonicPulseModelConfiger.getEntId());
            }
            if ("deviceid".equals(key) && temp.containsKey(columnKey)) {
                if (null != temp.get(columnKey)) {
                    sonicPulseModel.setDeviceId((String) temp.get(columnKey));
                }
            }
            if ("unit".equals(key) && temp.containsKey(columnKey)) {
                if (null != temp.get(columnKey)) {
                    sonicPulseModel.setUnit((String) temp.get(columnKey));
                }
            }
            if ("monitoringvalue".equals(key) && temp.containsKey(columnKey)) {
                if (null != temp.get(columnKey)) {
                    sonicPulseModel.setMonitoringValue(Double.valueOf(temp.get(columnKey).toString()));
                }
            }
            if ("poklad".equals(key) && temp.containsKey(columnKey)) {
                if (null != temp.get(columnKey)) {
                    sonicPulseModel.setPoklad(Double.valueOf(temp.get(columnKey).toString()));
                }
            }
            if ("datetime".equals(key)) {
                String[] columnTemp = columnKey.split("\\+");
                String dateTimeStr = "";
                for (String value : columnTemp) {
                    if (temp.containsKey(value)) {
                        dateTimeStr += temp.get(value) + " ";
                    }
                }
                dateTimeStr = dateTimeStr.trim();
                sonicPulseModel.setDatetime(DateUtils.fomatDateTime(dateTimeStr));
            }
            if ("workingface".equals(key) && temp.containsKey(columnKey)) {
                if (null != temp.get(columnKey)) {
                    sonicPulseModel.setWorkingFace((String) temp.get(columnKey));
                }
            }
            if ("smallestrange".equals(key) && temp.containsKey(columnKey)) {
                if (null != temp.get(columnKey)) {
                    sonicPulseModel.setSmallestRange(Double.valueOf(temp.get(columnKey).toString()));
                }
            }
            if ("maximumrange".equals(key) && temp.containsKey(columnKey)) {
                if (null != temp.get(columnKey)) {
                    sonicPulseModel.setMaximumRange(Double.valueOf(temp.get(columnKey).toString()));
                }
            }
            if ("stationname".equals(key) && temp.containsKey(columnKey)) {
                if (null != temp.get(columnKey)) {
                    sonicPulseModel.setStationName((String) temp.get(columnKey));
                }
            }
            if ("status".equals(key) && temp.containsKey(columnKey)) {
                if (null != temp.get(columnKey)) {
                    int status = Integer.parseInt(temp.get(columnKey).toString());
                    if (status <= 0) {
                        sonicPulseModel.setStatus(false);
                    } else {
                        sonicPulseModel.setStatus(true);
                    }
                }
            }
            if ("powercutarea".equals(key) && temp.containsKey(columnKey)) {
                if (null != temp.get(columnKey)) {
                    sonicPulseModel.setPowerCutArea((String) temp.get(columnKey));
                }
            }
            if ("powerreconnectionlimit".equals(key) && temp.containsKey(columnKey)) {
                if (null != temp.get(columnKey)) {
                    sonicPulseModel.setPowerReconnectionLimit(Double.valueOf(temp.get(columnKey).toString()));
                }
            }
            if ("powercutlimit".equals(key) && temp.containsKey(columnKey)) {
                if (null != temp.get(columnKey)) {
                    sonicPulseModel.setPowerCutLimit(Double.valueOf(temp.get(columnKey).toString()));
                }
            }
        }
        return sonicPulseModel;
    }



    public int readHistroyDataCount(Map<String, Object> conditions) {
        return sonicPulseDao.readHistroyDataCount(conditions);
    }

    public List<SonicPulseModel> readHistroyData(Map<String, Object> conditions, int start, int prePage) {
        List<SonicPulseModel> readHistroyDataModel = new ArrayList<SonicPulseModel>();
        Map<String, String> structure = sonicPulseModelConfiger.getDatakey();
        List<Map<String, Object>> readHistroyDatas = sonicPulseDao.readHistroyData(conditions, start, prePage);
        for (Map<String, Object> readHistroyData : readHistroyDatas) {
            SonicPulseModel sonicPulseModel = this.transformData(readHistroyData, structure);
            readHistroyDataModel.add(sonicPulseModel);
        }
        return readHistroyDataModel;
    }
}
